<?php
namespace app\model;



class Record {


    static function save($data){
        global $_G;
        $insertid = db_insert(t_record, $data);
        $id = $insertid;
        return $id;
    }

static function refesh($id,$themeid,$config,$system,$theme) {
        global $_G;

    $refundno = getMillisecond();
    $record = Record::getById($id);

    $isworker = Worker::getThemeWorkerByOpenid($record['openid'],$themeid,1);

    if (!empty($theme['jiezhang'])){
        return_msg('此活动已结算,无法补发');
    }
    $totalpayfee = Order::getRealByThemeIdnew($themeid) + $theme['chongzhi'];
    if ($totalpayfee<1){
        return_msg('您的余额不足,无法补发');
    }

    if ($config['iscomm'] != 1){
        return_msg('红包功能未开启');
    }

    $trade = Order::getPayOrderByRIdZhifude($id);
    if (empty($trade)){
        return array('未支付/已退/线下定单/虚拟定单的定单均不可补发红包');
    }
        
    if (empty($record['fopenid'])){
        return array('没有推荐人');
    }

    if ($record['fopenid'] == $record['openid'] && empty($isworker)){
        return array('推荐人为自己,无法补发');
    }

    // if (empty($config['comm11']) || empty($config['comm1'])){
    //  return_msg('红包金额未设置');
    // }


        $errortxt="";
        //员工提成
        if($config['iscomm'] == 1 && $system['fenxiao06'] == 1 && !empty($config['comm61']) && !empty($record['workerid'])){

            if ($system['jiage']==0 || ($system['jiage']>0 && $record['jiagetype']==$config['joinname'] )){
                $randomfee = floatval(mt_rand($config['comm61']*100,$config['comm62']*100)/100);
            }elseif ($system['jiage']>0 && $record['jiagetype']==$config['joinname2']) {
                $randomfee = floatval(mt_rand($config['comm61_jiage2']*100,$config['comm62_jiage2']*100)/100);
            }elseif ($system['jiage']>1 && $record['jiagetype']==$config['joinname3']) {
                $randomfee = floatval(mt_rand($config['comm61_jiage3']*100,$config['comm62_jiage3']*100)/100);
            }

            $pm = array(":themeid"=>$record['themeid'],":uid"=>$record['workerid']);
            $sql = "SELECT * FROM " . tablename (t_worker) . " 
                  WHERE themeid =:themeid  and uid = :uid order by id desc limit 1";
            $workerinfo = db_find ($sql,$pm);

            $sql = "SELECT ord.* FROM " . tablename ( t_comm ) . " as ord
                        WHERE  themeid ='{$themeid}' and ord.type=6 and ord.openid='" .$workerinfo['openid']."' and ord.fopenid='" .$record['openid']."'";
            $isorder = db_find ($sql);
            
            if (empty($isorder)){
                if($randomfee >= 0.3){
                   $recommed2 = Comm::WorkerRecommendCommAfterPayYxiaun($config,$record,$randomfee);
                   writelogcz($system,$themeid,'补发红包',"员工" . $workerinfo['username']. "的提成:" . $randomfee ."元 来自姓名:" . $record['username']."电话:" . $record['mobile'],$_G['username']);
                }
            }else{
                $errortxt = $errortxt . "|员工红包已发过";
            }

        }
        //员工没有红包
        $workerhb = Worker::getThemeWorkerByOpenid($record['fopenid'],$themeid,1);
        if (!empty($workerhb) && $config['commworker']==1){
            $workerhb=1;
        }else{
            $workerhb=0;
        }
        $isworker = Worker::getThemeWorkerByOpenid($record['openid'],$themeid,1);
        if($config['iscomm'] == 1 && $workerhb==0 && $system['fenxiao01'] > 0 && !empty($record['fopenid']) && ($record['fopenid'] != $record['openid'] || !empty($isworker)) && !empty($config['comm11']) && !empty($config['comm1'])){
            if ($system['jiage']==0 || ($system['jiage']>0 && $record['jiagetype']==$config['joinname'] )){

                //新版概率随机
                $first = intval($config['comm13']);
                $second= $first + intval($config['comm13_2']);
                $gailv = mt_rand(0,99);
                if ($first==0 || $first==100 || $gailv < $first ) {
                    $randomfee = floatval(mt_rand($config['comm11']*100,$config['comm12']*100)/100);
                }elseif ($gailv < $second && $config['comm12_2']!=0 ) {
                    $randomfee = floatval(mt_rand($config['comm11_2']*100,$config['comm12_2']*100)/100);
                }elseif ($config['comm12_3']!=0 ) {
                    $randomfee = floatval(mt_rand($config['comm11_3']*100,$config['comm12_3']*100)/100);
                }else{
                    $randomfee = floatval(mt_rand($config['comm11']*100,$config['comm12']*100)/100);
                }
            }elseif ($system['jiage']>0 && $record['jiagetype']==$config['joinname2']) {
                $randomfee = floatval(mt_rand($config['comm11_jiage2']*100,$config['comm12_jiage2']*100)/100);
                if($record['type'] == 1){
                    $randomfee = floatval(mt_rand($config['comm1_jiage2']*100,$config['comm2_jiage2']*100)/100);
                }
            }elseif ($system['jiage']>1 && $record['jiagetype']==$config['joinname3']) {
                $randomfee = floatval(mt_rand($config['comm11_jiage3']*100,$config['comm12_jiage3']*100)/100);
                if($record['type'] == 1){
                    $randomfee = floatval(mt_rand($config['comm1_jiage3']*100,$config['comm2_jiage3']*100)/100);
                }
            }

            $sql = "SELECT ord.* FROM " . tablename ( t_comm ) . " as ord
                        WHERE themeid ='{$themeid}' and ord.type=". $record['type']." and ord.openid='" .$record['fopenid']."' and ord.fopenid='" .$record['openid']."'";
            $isorder = db_find ($sql);

            if (empty($isorder)){
                if($randomfee >= 0.3){
                   $recommed = Comm::dealRecommendCommAfterPay($config,$record,$randomfee);
                   $fansinfo = Fans::get($record['fopenid']);
                   writelogcz($system,$themeid,'补发红包', $fansinfo['username']. "的一级分销:" . $randomfee ."元 来自姓名:" . $record['username']."电话:" . $record['mobile'],$_G['username']);
                }
            }else{
                $errortxt = $errortxt . "|一级红包已发过";
            }


            if (!empty($recommed)) {
            //员工没有红包
            $relation2 = Relation::getByThemeId($themeid,$record['fopenid']);
            $workerhb = Worker::getThemeWorkerByOpenid($relation2['popenid'],$themeid,1);
            if (!empty($workerhb) && $config['commworker']==1){
                $workerhb=1; 
            }else{
                $workerhb=0;
            }
            $isworker = Worker::getThemeWorkerByOpenid($record['fopenid'],$themeid,1);
            if($workerhb==0 && empty($isworker) && !empty($config['comm21'] && $system['fenxiao01'] > 1)){
                if ($system['jiage']==0 || ($system['jiage']>0 && $record['jiagetype']==$config['joinname'] )){
                    //新版概率随机
                    $first = intval($config['comm23']);
                    $gailv = mt_rand(0,99);
                    if ($first==0 || $first==100 || $gailv < $first ) {
                        $randomfee2 = floatval(mt_rand($config['comm21']*100,$config['comm22']*100)/100);
                    }elseif ($config['comm22_2']!=0 ) {
                        $randomfee2 = floatval(mt_rand($config['comm21_2']*100,$config['comm22_2']*100)/100);
                    }else{ 
                        $randomfee2 = floatval(mt_rand($config['comm21']*100,$config['comm22']*100)/100);
                    }
                }elseif ($system['jiage']>0 && $record['jiagetype']==$config['joinname2']) {
                    $randomfee2 = floatval(mt_rand($config['comm21_jiage2']*100,$config['comm22_jiage2']*100)/100);
                }elseif ($system['jiage']>1 && $record['jiagetype']==$config['joinname3']) {
                    $randomfee2 = floatval(mt_rand($config['comm21_jiage3']*100,$config['comm22_jiage3']*100)/100);
                }
                if($randomfee2 >=0.3){
                    if(!empty($relation2['popenid']) && $relation2['popenid'] != $record['openid'] && $relation2['popenid'] != $record['fopenid']){
                        //执行锁定级别
                        if( $config['isfreeze']==1){
                            $relationtable = Relation::getByThemeId($themeid,$record['fopenid']);
                            if(!empty($relationtable)){
                                $data = array("isfrozen"=>1);
                                Relation::update($relationtable['id'],$data);
                            }
                        }
                        $sql = "SELECT ord.* FROM " . tablename ( t_comm ) . " as ord
                                    WHERE themeid ='{$themeid}' and ord.type=2 and ord.openid='" .$relation2['popenid']."' and ord.fopenid='" .$record['openid']."'";
                        $isorder = db_find ($sql);
                        if (empty($isorder)){
                            $recommed2 = Comm::dealLevel2RecommendCommAfterPayYixuan($config,$record,$randomfee2,$relation2);

                            $fansinfo = Fans::get($relation2['popenid']);
                            writelogcz($system,$themeid,'补发红包', $fansinfo['username']. "的二级分销:" . $randomfee2 ."元 来自姓名:" . $record['username']."电话:" . $record['mobile'],$_G['username']);
                        }else{
                            $errortxt = $errortxt . "|二级红包已发过";
                        }
                        if (!empty($recommed2)) {
                        //员工没有红包                                
                        $relation3 = Relation::getByThemeId($themeid,$relation2['popenid']);
                        $workerhb = Worker::getThemeWorkerByOpenid($relation3['popenid'],$themeid,1);
                        if (!empty($workerhb) && $config['commworker']==1){
                            $workerhb=1;
                        }else{
                            $workerhb=0;
                        }
                        $isworker = Worker::getThemeWorkerByOpenid($relation2['popenid'],$themeid,1);
                        if($workerhb==0 && empty($isworker) &&  $recommed2>0 && $system['fenxiao01'] == 3 && !empty($config['comm31']) && !empty($relation2['popenid'])){
                            if ($system['jiage']==0 || ($system['jiage']>0 && $record['jiagetype']==$config['joinname'] )){
                                //新版概率随机
                                $first = intval($config['comm33']);
                                $gailv = mt_rand(0,99);
                                if ($first==0 || $first==100 || $gailv < $first ) {
                                    $randomfee3 = floatval(mt_rand($config['comm31']*100,$config['comm32']*100)/100);
                                }elseif ($config['comm32_2']!=0 ) {
                                    $randomfee3 = floatval(mt_rand($config['comm31_2']*100,$config['comm32_2']*100)/100);
                                }else{ 
                                    $randomfee3 = floatval(mt_rand($config['comm31']*100,$config['comm32']*100)/100);
                                }
                            }elseif ($system['jiage']>0 && $record['jiagetype']==$config['joinname2']) {
                                $randomfee3 = floatval(mt_rand($config['comm31_jiage2']*100,$config['comm32_jiage2']*100)/100);
                            }elseif ($system['jiage']>1 && $record['jiagetype']==$config['joinname3']) {
                                $randomfee3 = floatval(mt_rand($config['comm31_jiage3']*100,$config['comm32_jiage3']*100)/100);
                            }

                            if($randomfee3 >=0.3){
                                //确定上一级不为空并且不是本人和1,2级
                                if(!empty($relation3['popenid']) && $relation3['popenid'] != $record['openid'] && $relation3['popenid'] != $relation2['popenid'] && $relation3['popenid'] != $record['fopenid']){
                                    //执行锁定级别
                                    if( $config['isfreeze']==1){
                                        $relationtable = Relation::getByThemeId($themeid,$relation2['popenid']);
                                        if(!empty($relationtable)){
                                            $data = array("isfrozen"=>1);
                                            Relation::update($relationtable['id'],$data);
                                        }
                                    }
                                    $sql = "SELECT ord.* FROM " . tablename ( t_comm ) . " as ord
                                                WHERE themeid ='{$themeid}' and ord.type=3 and ord.openid='" .$relation3['popenid']."' and ord.fopenid='" .$record['openid']."'";
                                    $isorder = db_find ($sql);
                                    if (empty($isorder)){
                                        $recommed3 = Comm::dealLevel3RecommendCommAfterPayYxiaun($config,$record,$randomfee3,$relation3);

                                        $fansinfo = Fans::get($relation3['popenid']);
                                        writelogcz($system,$themeid,'补发红包', $fansinfo['username']. "的三级分销:" . $randomfee3 ."元 来自姓名:" . $record['username']."电话:" . $record['mobile'],$_G['username']);
                                    }else{
                                        $errortxt = $errortxt . "|三级红包已发过";
                                    }
                                    
                                }
                            }
                        }
                        }//comm31

//                        if($recommed2 > 0){
//                            $fmember2 = Fans::getByTheme($record['themeid'],$record['fopenid']);
//                            $record['fopenid'] = $relation2['popenid'];
//                            self::recommedCommNotice($url.'&randomfee='.$randomfee2,$record,$system,$fmember2['nickname'],$theme,2);
//                        }
                    }
                }

            }
            }
            return array('补发完成'.$errortxt, $record['username']);
        }else{
            $errortxt="";
            if ($workerhb!=0) {
                $errortxt .="员工没有红包，";
            }
            if ($fenxiao01==0) {
                $errortxt .="分销红包未开启，";
            }
            if (empty($record['fopenid'])) {
                $errortxt .="推荐人为空，";
            }
            if ($record['fopenid'] != $record['openid']) {
                $errortxt .="推荐人是自己，";
            }
            if (empty($config['comm11']) || empty($config['comm1'])) {
                $errortxt .="一级分销红包未配置";
            }
            return array('无法补发：'.$errortxt);
        }
    }

    static function ispay($config,$uid,$themeid){
        $p3 = array(":uid"=>$uid,":themeid"=>$themeid);
        if ($config['joinfee']=='0.00' && $config['groupprice']=='0.00'){
            $friendorder = db_find ( "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and uid = :uid and type<>8 order by id desc limit 1",$p3);
        }else{
            $friendorder = db_find ( "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and uid = :uid and (status > 0 or status=-2)  order by id desc limit 1",$p3);
        }
        return $friendorder;
    }

    static function update($id,$data){
        global $_G, $_P;
        if($id <= 0){
            return false;
        }
        return db_update (t_record, $data, array("id"=>$id) );
    }

    static function searchtuan($themeid){
        global $_G, $_P;
        
        $txt = trim($_P['txt']);
        $tuanlist = db_select('SELECT * FROM ' . tablename(t_record) . ' 
                WHERE themeid = :themeid AND status <> 0 and type = 0 and (username like "%' . $txt . '%" or mobile like "%' . $txt . '%")', array( ':themeid' => $themeid));
        if (count($tuanlist)==0) {
            $result = array(
                'errcode' => 0, 
                'msg' => '未搜索到团长-' . $txt, 
            );
        }else{
            foreach ($tuanlist as $index => $row) {
                $tuanlist[$index]['renshu'] = Record::getAttendDetailNumByPId($row['id'])+1;
                $usernamelen=mb_strlen($row['mobile'],'utf-8');
                $tuanlist[$index]['mobile']=mb_substr($row['mobile'],0,3,'utf-8').'****'.mb_substr($row['mobile'],$usernamelen-4,4,'utf-8');

            }
            if (count($tuanlist)==1) {
                $result = array(
                    'errcode' => 1, 
                    'tuanlist' => $tuanlist, 
                    'msg' => '成功', 
                );
            }else{
                $result = array(
                    'errcode' => 2, 
                    'tuanlist' => $tuanlist, 
                    'msg' => '多条记录', 
                );
            }
        }
        return $result;
    }

    static function getPayedGroupById($id,$themeid) {
        if (empty($id) || empty($themeid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
    									WHERE themeid =:themeid
    											and id = :id and (status > 0 or status =-2) and type = 0";
        $row = db_find($sql, array(":themeid"=>$themeid,":id"=>$id));
        return $row;
    }

    static function getIngByOpenId($openid,$themeid) {
        if (empty($openid) || empty($themeid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid and openid = :openid and type = 0 and status > 0 and isfinish=0 order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getNumByThemeId($themeid,$touxiang,$showalldata){
        global $_G;
        if($themeid <= 0){
            return false;
        }
        $params = array();
        if ($showalldata==1) {
            if ($touxiang==1){
                //资料
                $sql = "SELECT COUNT(*) FROM  " . tablename ( t_fans ) . " WHERE  username<>''";
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT COUNT(*) FROM " . tablename ( t_record ) . "
                WHERE status > 0 ";
            }else{
                //查看
                $sql = "SELECT COUNT(*) FROM  " . tablename ( t_fans ) . " ";
            }
        }else{
            $params[':themeid'] = $themeid;
            if ($touxiang==1){
                //资料
                $sql = "SELECT COUNT(*) FROM  " . tablename ( t_fans ) . " WHERE   themeid =:themeid and username<>''";
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT COUNT(*) FROM " . tablename ( t_record ) . "
                WHERE themeid =" . $themeid . "  and status > 0 ";
            }else{
                //查看
                $sql = "SELECT COUNT(*) FROM  " . tablename ( t_fans ) . " WHERE   themeid =:themeid";
            }
         }
       $num = db_fetchcolumn($sql,$params);
        return intval($num);
    }

    static function getFansNumByThemeId($themeid,$showalldata){

        if ($showalldata==1) {
            $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fans ) );
        }else{
            $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_fans ) . "
                    WHERE  themeid = :themeid ", array(":themeid"=>$themeid) );
        }
        return intval($num);
    }

    static function getGroupsNumByThemeId($themeid,$showalldata){

        if ($showalldata==1) {
            $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
                    WHERE  type = 0 and (status > 0 or status=-2)");
        }else{
            $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . "
                    WHERE  themeid = :themeid and type = 0 and (status > 0 or status=-2)", array(":themeid"=>$themeid) );
        }
        return intval($num);
    }

    static function getscannumByThemeId($themeid,$showalldata){
        global $_G;
        if ($showalldata==1) {
            $scansum = db_fetchcolumn ( 'SELECT sum(scansum) FROM ' . tablename ( t_theme ) . " 
                    WHERE  uniacid= :uniacid " );
        }else{
            $sql = "SELECT * FROM " . tablename(t_theme) . " where id = :id";
            $row = db_find($sql, array(":id"=>$themeid));
            $scansum = $row['scansum'];
        }
        return intval($scansum);
    }

    static function getforwardnumByThemeId($themeid,$showalldata){
        global $_G;
        if ($showalldata==1) {
            $scansum = db_fetchcolumn ( 'SELECT sum(forward) FROM ' . tablename ( t_theme ) . " 
                    WHERE  uniacid= :uniacid " );
        }else{
            $sql = "SELECT * FROM " . tablename(t_theme) . " where id = :id";
            $row = db_find($sql, array(":id"=>$themeid));
            $scansum = $row['forward'];
        }
        return intval($scansum);
    }

    static function getFanslistByThemeId($themeid,$touxiang,$limit,$showalldata){
        global $_G;
        if($themeid <= 0){
            return false;
        }
        $params = array();

        if ($showalldata==1) {
            if ($touxiang==1){
                //资料
                $sql = "SELECT username,avatar FROM ".tablename(t_fans)."
                            WHERE username<>'' order by id desc limit ".$limit;
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT username,avatar FROM ".tablename(t_record)."
                            WHERE  status > 0 and uid > 0 order by id desc limit ".$limit;
            }else{
                //查看
                $sql = "SELECT nickname as username,avatar FROM ".tablename(t_fans)."
                             order by id desc limit ".$limit;
            }
        }else{
            $params[':themeid'] = $themeid;
            if ($touxiang==1){
                //资料
                $sql = "SELECT username,avatar FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid and username<>'' order by id desc limit ".$limit;
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT username,avatar FROM ".tablename(t_record)."
                            WHERE themeid =:themeid and status > 0 and uid > 0 order by id desc limit ".$limit;
            }else{
                //查看
                $sql = "SELECT nickname as username,avatar FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid order by id desc limit ".$limit;
            }
        }
        $fanslist = db_select($sql,$params);
        return $fanslist;
    }
    static function getFanslistByThemeIdall($themeid,$touxiang,$limit,$showalldata){
        global $_G;
        if($themeid <= 0){
            return false;
        }
        $params = array();
        if ($showalldata==1) {
            if ($touxiang==1){
                //资料
                $sql = "SELECT username,avatar,mobile,addtime FROM ".tablename(t_fans)."
                            WHERE username<>'' order by id desc limit ".$limit;
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT username,avatar,mobile,fee,addtime FROM ".tablename(t_record)."
                            WHERE status > 0 and uid > 0 order by id desc limit ".$limit;
            }else{
                //查看
                $sql = "SELECT nickname as username,avatar,mobile,addtime FROM ".tablename(t_fans)."
                            order by id desc limit ".$limit;
            }
            $fanslist = db_select($sql);
        }else{
            $params[':themeid'] = $themeid;
            if ($touxiang==1){
                //资料
                $sql = "SELECT username,avatar,mobile,addtime FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid and username<>'' order by id desc limit ".$limit;
            }elseif ($touxiang==2){
                //支付人数
                $sql = "SELECT username,avatar,mobile,fee,addtime FROM ".tablename(t_record)."
                            WHERE themeid =:themeid and status > 0 and uid > 0 order by id desc limit ".$limit;
            }else{
                //查看
                $sql = "SELECT nickname as username,avatar,mobile,addtime FROM ".tablename(t_fans)."
                            WHERE themeid =:themeid order by id desc limit ".$limit;
            }
            $fanslist = db_select($sql,$params);
        }
        return $fanslist;
    }

    //删除或退单更新数据
    static function deleterecord($system,$themeid,$id){
        global $_G;
        $sql = "SELECT * FROM " . tablename (t_record) . " WHERE themeid =:themeid and id = :id";
        $record = db_find($sql, array(":themeid"=>$themeid,":id"=>$id));
        if ($record['status']==1){
            if ($system['softstyle']==5) {
                //加盟取消锁定
                $state = db_update(t_shengshi, array('recordid' => 0), array('areaId' => $record['address']));
            }

            db_query("UPDATE ".tablename(t_worker)." SET paycount = paycount - 1 WHERE themeid = '{$themeid}' and uid ={$record['workerid']}");

            db_query("UPDATE ".tablename(t_fans)." SET tuijian = tuijian - 1 WHERE themeid = '{$themeid}' and uid ={$record['fromuid']}");
            // db_update(t_report, array('refund' => $record['addtime']), array( 'software' => MODULE_NAME ,'uniacid' => $_G['mpid'] , 'themeid' => $themeid , 'riqi' => date('Y-m-d')));
            $today = date('Y-m-d') ;
            $daylist = db_find("SELECT * FROM " . tablename ( t_report ) . " WHERE software = '".MODULE_NAME."' and themeid = '{$themeid}' and riqi = '" . $today ."'");
            if (empty($daylist)) {
                $data = array(
                    'software' => MODULE_NAME,
                    'uniacid' => $_G['mpid'] , 
                    'themeid' => $themeid , 
                    'riqi' => $today , 
                    'renewtime' => TIMESTAMP , 
                    'refund' => 1 , 
                    'refundfee' => $record['fee'] ,            
                ); 
                $insertid = db_insert(t_report,$data);
            }else{
                db_query("UPDATE ".tablename(t_report)." SET refund = refund + 1,refundfee = refundfee + " . $record['fee'] . " WHERE software = '" .MODULE_NAME . "' and themeid = " . $themeid . " and riqi ='" . date('Y-m-d') ."'");

            }
            Theme::workerreport($themeid,$record['workerid'],array('refund' =>1));
            Theme::workerreport($themeid,$record['workerid'],array('refundfee' =>$record['fee']));
            Theme::workerreport($themeid,$record['workerid'],array('record' =>-1));
            if ($record['workerid'] == $record['fromuid']) {
                Theme::workerreport($themeid,$record['workerid'],array('recordzx' =>-1));
            }
            Theme::workerreport($themeid,$record['workerid'],array('fee' =>-$record['fee']));
            db_query("UPDATE ".tablename(t_fans)." SET tuijian = tuijian - 1 WHERE themeid = '{$themeid}' and uid ={$record['fromuid']}");
            //自动更新报表
            Theme::renewworkreport($themeid,$record['workerid']);

            $p = array(":themeid"=>$themeid);
            db_query("UPDATE ".tablename(t_theme)." SET recordcount = recordcount - 1,ordercount = ordercount - " . $record['fee'] . " WHERE uniacid = :uniacid and id =:themeid",$p);
        }elseif ($record['status']==3){
            db_query("UPDATE ".tablename(t_workerreport)." SET recordxx = recordxx - 1 WHERE themeid = ".$themeid." and workerid = " . $record['workerid'] ." and riqi ='" . date("Y-m-d") ."'");
        }

    }

    static function getFanslistByThemeIdjm($themeid,$touxiang,$limit){
        global $_G;
        if($themeid <= 0){
            return false;
        }
        $params = array();
        $params[':themeid'] = $themeid;
        if ($touxiang==1){
            //资料
            $sql = "SELECT username,avatar FROM ".tablename(t_fans)."
                        WHERE  themeid =:themeid and username<>'' order by id desc limit ".$limit;
        }elseif ($touxiang==2){
            //支付人数
            $sql = "SELECT username,avatar,mobile,addtime,fee FROM ".tablename(t_record)."
                        WHERE  themeid =:themeid and status > 0 and uid > 0 order by id desc limit ".$limit;
        }else{
            //查看
            $sql = "SELECT nickname as username,avatar FROM ".tablename(t_fans)."
                        WHERE  themeid =:themeid order by id desc limit ".$limit;
        }
        $fanslist = db_select($sql,$params);
        return $fanslist;
    }



    static function getPayedNumByThemeId($themeid){
        if($themeid <= 0){
            return false;
        }
        $pm = array(":themeid"=>$themeid);
        $sql = "SELECT COUNT(*) FROM  " . tablename ( t_record ) . " WHERE  themeid =:themeid and status > 0";
        $num = db_fetchcolumn($sql,$pm);
        return intval($num);
    }

    static function getHasAttendNumByThemeId($themeid){
        if($themeid <= 0){
            return false;
        }
        $pm = array(":themeid"=>$themeid);
        $sql = "SELECT COUNT(*) FROM  " . tablename ( t_record ) . " WHERE  themeid =:themeid and type = 0 and status > 0";
        $num = db_fetchcolumn($sql,$pm);
        return intval($num);
    }


    static function getLastGroupByOpenId($openid,$themeid) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid
											and openid = :openid and (status > 0 or status=-2) and type<>8 order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }
    static function getLastGroupByUid($uid,$themeid) {
        if (empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid
											and uid = :uid and (status > 0 or status=-2) and type<>8 order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":uid"=>$uid));
        return $row;
    }
    static function getLastGroupByOpenIduppaid($openid,$themeid) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and openid = :openid and status = 0 order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getAttendGroupByOpenId($openid,$themeid) {
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE themeid =:themeid 
											and openid = :openid and type = 0 and (status > 0 or status=-2) order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getAttendGroupListByOpenId($openid,$themeid){
        $sql =  "SELECT * FROM  " . tablename ( t_record ) . "
					WHERE themeid =:themeid  and type = 0 and (status > 0 or status=-2) and openid = :openid order by id desc";
        $list = db_select ($sql,array(":themeid"=>$themeid,":openid"=>$openid));
        return $list;

    } 


    static function getAttendGroupById($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
									WHERE id = :id and (status > 0 or status=-2) and type = 0";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }

    static function getAttendGroupByIdTuan($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename (t_record) . "
                                    WHERE id = :id and status = -1 and type = 0";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }

    static function getMyDetailList($openid,$themeid){
        $sql =  "SELECT * FROM  " . tablename ( t_record ) . "
					WHERE themeid =:themeid  and type = 1 and status > 0 and openid = :openid order by id desc";
        $list = db_select ($sql,array(":themeid"=>$themeid,":openid"=>$openid));
        return $list;

    }

    static function getAllDetailListByPidAndStatus($pid,$status){
        $con = "type = 1 and status >= 0 and pid = :pid";
        $p = array(":pid"=>$pid);
        if($status >= 0){
            $con.=" and status = :status";
            $p[':status'] = $status;
        }
        $sql =  "SELECT * FROM  " . tablename ( t_record ) . "
					WHERE {$con} order by id desc";
        $list = db_select ($sql,$p);
        return $list;
    }

    static function getAllDetailListByPid($pid){
        $sql =  "SELECT * FROM  " . tablename ( t_record ) . "
					WHERE type = 1 and status >= 0 and pid = :pid order by id desc";
        $list = db_select ($sql,array(":pid"=>$pid));
        return $list;
    }

    static function getDetailListByPid($pid){
        $sql =  "SELECT avatar,username FROM  " . tablename ( t_record ) . "
					WHERE type = 1 and status > 0 and pid = :pid order by id asc";

        $list = db_select ($sql,array(":pid"=>$pid));
        return $list;
    }

    static function getLimitById($themeid,$limit=70) {
        if (empty($themeid)) {
            return false;
        }
        $sql = "SELECT id,nickname,avatar FROM ".tablename(t_record)." as f
					WHERE themeid ='{$themeid}' and status > 0 group by uid order by id desc limit ".$limit;
        $fakelist = db_select($sql);
        return $fakelist;
    }
    static function getLimitByIdnew($themeid,$limit=70) {
        if (empty($themeid)) {
            return false;
        }
        $sql = "SELECT id,nickname,avatar,uid as fansuid FROM ".tablename(t_record)." as f
                    WHERE themeid ='{$themeid}' and status > 0 and uid > 0 order by id desc limit ".$limit;
        $fakelist = db_select($sql);
        return $fakelist;
    }
    static function getById($id) {
        if (empty($id)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where id = :id";
        $row = db_find($sql, array(":id"=>$id));
        return $row;
    }

    static function setCheckById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("hascheck"=>1), array ('id' => $id) );
    }

    static function setIsfinish($id) {
        if (empty($id)) {
            return false;
        }
        db_update( t_record, array("isfinish"=>1), array ('pid' => $id) );
        return db_update( t_record, array("isfinish"=>1), array ('id' => $id) );
    }

    static function setNofinish($id) {
        if (empty($id)) {
            return false;
        }
        db_update( t_record, array("isfinish"=>0), array ('pid' => $id) );
        return db_update( t_record, array("isfinish"=>0), array ('id' => $id) );
    }

    static function setHasNoticeById($id,$hasnotice = 1) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("hasnotice"=>$hasnotice), array ('id' => $id) );
    }

    static function setCostAndLeftFeeById($costfee,$leftfee,$sendfee=0,$id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("costfee"=>$costfee,"leftfee"=>$leftfee,"sendfee"=>$sendfee), array ('id' => $id) );
    }

    static function setRefundStatusById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("status"=>-1), array ('id' => $id) );
    }

    static function setIsDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("isdistributor"=>1), array ('id' => $id) );
    }

    static function setEndDistributorById($id) {
        if (empty($id)) {
            return false;
        }
        return db_update( t_record, array("isdistributor"=>2), array ('id' => $id) );
    }

    static function deleteById($id){
        if (empty($id)) {
            return false;
        }
        db_delete ( t_record, array ('id' => $id) );
        return true;
    }
    static function getShowList($themeid,$shownum){
        global $_G, $_P;
        // $cachekey = 'show'.$_G['mpid'].MODULE_NAME.$themeid;
        // $settings = cache_load($cachekey);
        // if(empty($settings)){
            $p =  array(":themeid"=>$themeid);
            $settings = db_select("SELECT * FROM ".tablename(t_record)." as f
				WHERE themeid =:themeid and status != 0 group by uid order by id desc limit ".$shownum,$p);
        //     if(!empty($settings)){
        //         cache_write($cachekey, $settings);
        //     }
        // }
        return $settings;
    }

    static function getListByCondition($condition,$pageNumber,$pageSize){
        $sql = "SELECT master.* FROM " . tablename ( t_record ) . " as master 
						WHERE   {$condition} ORDER BY master.id desc
									LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
        $list = db_select ( $sql );
        return $list;

    }

    static function getAttendNumByThemeIdAndStatus($themeid,$status = 1){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }



    static function getIngListByThemeId($themeid,$limit=20){
        $sql = "SELECT * FROM " . tablename ( t_record ) . " 
					WHERE type = 0 and themeid = :themeid and status = 1 and isfinish = 0 ORDER BY id desc limit ".$limit;
        $list = db_select ( $sql , array(":themeid"=>$themeid) );
        return $list;
    }

    static function getPayEdListByThemeId($themeid,$limit=20){
        $sql = "SELECT * FROM " . tablename ( t_record ) . " 
					WHERE type = 0 and themeid = :themeid and status >= 1 ORDER BY id desc limit ".$limit;
        $list = db_select ( $sql , array(":themeid"=>$themeid) );
        return $list;
    }

    static function getPayEdListByThemeIding($themeid,$limit=20){
        $sql = "SELECT * FROM " . tablename ( t_record ) . " 
                    WHERE type = 0 and themeid = :themeid and status >= 1 and isfinish=0 and avatar is not null and avatar<>'' ORDER BY id desc limit ".$limit;
        $list = db_select ( $sql , array(":themeid"=>$themeid) );
        return $list;
    }

    static function getAttendDetailNumByPId($pid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  type = 1 and (status > 0 or status = -2) and pid = :pid", array(":pid"=>$pid) );
        return intval($num);
    }

    static function getAttendNumByThemeId($themeid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  themeid = :themeid and status > 0 ORDER BY master.id", array(":themeid"=>$themeid) );
        return intval($num);
    }

    static function getByThemeIdAndCode($themeid,$code){
        $rcode = db_find ( "SELECT * FROM ".tablename(t_record)."
            			WHERE themeid = :themeid and code = :code",array(":themeid"=>$themeid,":code"=>$code));
        return $rcode;
    }


    static function getFirstAttendByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_record ) . " as master 
						WHERE   themeid = :themeid and status = 1 ORDER BY master.id asc LIMIT 1";
        $list = db_find ($sql , array(":themeid"=>$themeid));
        return $list;
    }

    static function setFirstAttendOut($orderid){
        $list = db_update (t_record,array('status'=>2) , array("id"=>$orderid,"status"=>1));
        return $list;
    }

    static function getAttendListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_record ) . " as master 
						WHERE   themeid = :themeid and status > 0 ORDER BY master.id desc LIMIT 16";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }


    static function getNumByThemeIdAndStatus($themeid,$status=0){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  themeid = :themeid and status = :status ORDER BY master.id", array(":themeid"=>$themeid,":status"=>$status) );
        return intval($num);
    }

    static function getOutListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_record ) . " as master 
						WHERE   themeid = :themeid and status = 2 ORDER BY master.id LIMIT 20";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }

    static function getInListByThemeId($themeid){
        $sql = "SELECT master.* FROM " . tablename ( t_record ) . " as master 
						WHERE   themeid = :themeid and status = 1 ORDER BY master.id LIMIT 30";
        $list = db_select ( $sql , array(":themeid"=>$themeid));
        return $list;

    }

    static function getNumByOpenid($openid){
        $num = db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  openid = :openid ORDER BY master.id", array(":openid"=>$openid) );
        return intval($num);
    }

    static function getNumByCondition($condition){
        return db_fetchcolumn ( 'SELECT COUNT(*) FROM ' . tablename ( t_record ) . " as master
					WHERE  {$condition} ORDER BY master.id" );

    }




    static function getByIdAndShopid($id,$shopid) {
        if (empty($id) || empty($shopid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where id = :id and shopid = :shopid";
        $row = db_find($sql, array(":id"=>$id,":shopid"=>$shopid));
        return $row;
    }

    static function getAttendListLimit20($themeid){
        if (empty($themeid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " 
                        where themeid =:themeid and status != 0 ORDER BY id desc LIMIT 20";
        $p = array(":themeid"=>$themeid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getUnAttendByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where themeid = :themeid and openid = :openid and status = 0  order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getAttendByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where themeid = :themeid and openid = :openid and status > 0  order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getAttendNumByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "count(*) FROM " . tablename(t_record) . " where themeid = :themeid and openid = :openid and status > 0  order by id desc limit 1";
        $row = db_fetchcolumn($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return intval($row);
    }

    static function getByThemeIdAndOpenid($themeid,$openid) {
        if (empty($themeid) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where status >=0 and themeid = :themeid and openid = :openid order by id desc limit 1";
        $row = db_find($sql, array(":themeid"=>$themeid,":openid"=>$openid));
        return $row;
    }

    static function getByIdAndOpenid($id,$openid) {
        if (empty($id) || empty($openid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " where id = :id and openid = :openid";
        $row = db_find($sql, array(":id"=>$id,":openid"=>$openid));
        return $row;
    }


    static function getFixListByOpenidOrMobile($openid,$mobile,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.type = 'repair' and r.uniacid = :uniacid and r.openid = :openid";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        if(!empty($mobile)){
            $sql.=" or r.regmobile = :mobile";
            $p[":mobile"] = $mobile;
        }
        $sql .= " order by r.id desc limit 50";

        $list = db_select($sql,$p );
        return $list;

    }


    static function getUnNoticeList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_record) . " as r
                        where r.uniacid = :uniacid and r.status = 0 and r.hasnotice = 0";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getCheckList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_record) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 0 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getDistributorList($uniacid){
        if (empty($uniacid)) {
            return false;
        }
        $sql = "SELECT r.* FROM " . tablename(t_record) . " as r
                        where r.uniacid = :uniacid and r.status >= 1 and r.fee > 0 and r.hascheck = 1 and r.isdistributor = 1 order by r.id desc";
        $p = array(":uniacid"=>$uniacid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getIsOpenListByShopid($shopid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.isopen = 1
                                and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByShopidAndWorker($shopid,$wopenid,$status,$uniacid){
        if (empty($shopid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.shopid =:shopid and r.wopenid =:wopenid and r.uniacid = :uniacid and r.status = :status order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":status"=>$status,":shopid"=>$shopid,":wopenid"=>$wopenid);
        $list = db_select($sql,$p );
        return $list;
    }


    static function getListByOpenidOrMobile($openid,$uniacid){
        if (empty($openid)) {
            return false;
        }
        $sql = "SELECT r.*,v.img,s.title as shop,s.mobile as kfdh FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        left join " . tablename(t_shop) . " as s on r.shopid = s.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.openid = :openid order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":openid"=>$openid);
        $list = db_select($sql,$p );
        return $list;
    }

    static function getListByMobile($mobile,$uniacid){
        if (empty($mobile)) {
            return false;
        }
        $sql = "SELECT r.*,v.img FROM " . tablename(t_record) . " as r
                        left join " . tablename(t_version) . " as v on r.versionid = v.id
                        where r.status >= 0 and r.uniacid = :uniacid and r.regmobile = :regmobile order by r.id desc limit 50";
        $p = array(":uniacid"=>$uniacid,":regmobile"=>$mobile);
        $list = db_select($sql,$p );
        return $list;

    }


    static function getCancelByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $p = array(":openid"=>$openid,":uniacid"=>$uniacid);
        $p[':addtime'] = TIMESTAMP - 7*24*60*60;
        $hascancel = db_fetchcolumn ( "SELECT COUNT(*) FROM " . tablename (t_record) . "
								WHERE uniacid = :uniacid and status = -1 and openid =:openid and addtime > :addtime",$p);
        return $hascancel;
    }

    static function getUndealFixByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " 
                    where uniacid = :uniacid and type = 'repair' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getUndealRecycleByOpenId($openid,$uniacid) {
        if (empty($openid) || empty($uniacid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename(t_record) . " 
                    where uniacid = :uniacid and type = 'recycle' and status = 0 and openid =:openid limit 1";
        $row = db_find($sql, array(":openid"=>$openid,":uniacid"=>$uniacid));
        return $row;
    }

    static function getTotalByStatus($status,$uniacid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_record ) . " WHERE uniacid = :uniacid and status = :status";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByShopid($status,$uniacid,$shopid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_record ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getTotalByStatusByWorker($status,$uniacid,$shopid,$openid){
        $p = array(":status"=>$status,":uniacid"=>$uniacid,":shopid"=>$shopid,":openid"=>$openid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_record ) . " WHERE uniacid = :uniacid and status = :status and shopid =:shopid and wopenid = :openid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }


    static function getTotalByShopid($shopid){
        if(empty($shopid)){
            return false;
        }
        $p = array(":shopid"=>$shopid);
        $sql =  "SELECT COUNT(*) FROM" . tablename ( t_record ) . " WHERE shopid = :shopid";
        $total = db_fetchcolumn ($sql,$p);
        return intval($total);
    }

    static function getByUId($uid) {
        if (empty($uid)) {
            return false;
        }
        $sql = "SELECT * FROM " . tablename("mc_members") . " where uid = :uid";
        $row = db_find($sql, array(":uid"=>$uid));
        return $row;
    }
    static function getByOpenid($openid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $fans = db_find("SELECT * FROM ".tablename(t_worker)." WHERE openid = :openid",$params);
        return $fans;
    }

    static function getShopWorkerByOpenid($openid,$shopid){
        global $_G, $_P;
        if(empty($openid)){
            $openid = $_G['openid'];
        }
        $params = array();
        $params[':openid'] = $openid;
        $params[':shopid'] = $shopid;
        $row = db_find("SELECT * FROM ".tablename(t_worker)." WHERE shopid = :shopid and openid = :openid",$params);
        return $row;
    }



}
